import htmlClient from './fetch'
import nativeClient from './native'
import qs from 'qs'

const urlParamsEncode = function(url,params){
    let result = url
    for (var key in params) {
      if (params.hasOwnProperty(key)) {
          result = result.replace(`{${key}}`,params[key])
      }
    }
    return result
  }

  
const client = function (req) { 
    req.url = urlParamsEncode(req.url,req.urlParams)
     
    req.url = req.url + qs.stringify(req.params, { addQueryPrefix: true })
    
    req.params = null
    if (window.os === 'ios') {
        return nativeClient(req)
    } else {
        return htmlClient(req)
    }
}

export default client